﻿using System;
using System.Collections.Generic;
using BuzzX8.Xmpp.StreamElements.Stanza;

namespace BuzzX8.Xmpp.StreamElements.Builders
{
    public class StanzaErrorBuilder : ErrorElementBuilder<StanzaError>
    {
        protected override IEnumerable<string> GetSupportedElementNames()
        {
            return new[] {"error"};
        }

        protected override StanzaError CreateNewElement(string name)
        {
            if (name == "error") return new StanzaError(StanzaErrorType.Auth);
            throw new NotImplementedException();
        }

        protected override void AddAttributeToRootElement(string name, string value)
        {
            switch (name)
            {
                case "by":
                    RootElement.ErrorGeneratorName = value;
                    break;

                case "type":
                    RootElement.ErrorType = GetErrorType(value);
                    break;
            }
        }

        private StanzaErrorType GetErrorType(string value)
        {
            switch (value)
            {
                case "auth":
                    return StanzaErrorType.Auth;

                case "cancel":
                    return StanzaErrorType.Cancel;

                case "continue":
                    return StanzaErrorType.Continue;

                case "modify":
                    return StanzaErrorType.Modify;

                case "wait":
                    return StanzaErrorType.Wait;

                default:
                    throw new NotImplementedException();
            }
        }
    }
}
